package com.jic.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.order.api.CardOrderApi;
import com.jic.order.request.base.CardCouponPlatformRequest;
import com.jic.order.request.base.OrderCouponPlatformRequest;
import com.jic.order.service.OrderGoodsService;
import com.jic.order.service.OrderParentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
@Slf4j
@RequestMapping("/cardOrder")
@Api(tags = "卡券平台")
public class CardOrderController {
    @Autowired
    private CardOrderApi orderApi;
    @Autowired
    private OrderParentService orderParentService;
    @Autowired
    private OrderGoodsService orderGoodsService;

    /**
     * @param cardCouponPlatformRequest
     * @return
     */
    @ApiOperation(value = "卡券下单")
    @PostMapping("/addCardCouponPlatform")
    @ResponseBody
    public RestResult<String> addCardCouponPlatform(@RequestBody CardCouponPlatformRequest cardCouponPlatformRequest) {
        RestResult<?> ret =  orderApi.addCardCouponPlatform(cardCouponPlatformRequest);
        JSONObject card = (JSONObject) ret.getData();
        JSONObject content = card.getJSONObject("content");
        String code = content.getString("code");
        String wzOrderNo = content.getString("orderNo");
        //商城订单号
        Long orderGoodId =  orderParentService.queryGoodByOrderNo(cardCouponPlatformRequest.getParentOrderNo());
        //根据id更新
        content.clear();
        content.put("card_order_no",wzOrderNo);
        content.put("card_status",2);
        content.put("id",orderGoodId);
        orderGoodsService.updateWzCardNo(content);
        content.clear();
        card.clear();
        log.info("卡券下单:{}",card);
        return  RestResult.success("下单成功");
    }

    /**
     * @param orderCouponPlatformRequest
     * @return
     */
    @ApiOperation(value = "卡券查询")
    @PostMapping("/getCardCouponPlatform")
    @ResponseBody
    public RestResult<String> getCardCouponPlatform(@RequestBody OrderCouponPlatformRequest orderCouponPlatformRequest) {
        RestResult<?> ret =  orderApi.getCardCouponPlatform(orderCouponPlatformRequest);
        JSONObject card = (JSONObject) ret.getData();
        log.info("卡券查询:{}",card);
        return  RestResult.success("卡券查询成功");
    }
}
